<template>
  <div>
    <el-dialog :title="info.isAdd ? '添加' : '编辑'" :visible.sync="info.isShow" @close="close">
      <div>{{ user }}</div>
      <el-form :model="user" label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="user.pid" placeholder="请选择">
             <el-option
              label="顶级分类"
              :value="0"
            > </el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
              :disabled="item.id == user.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="user.catename"></el-input>
        </el-form-item>
        <el-form-item label="图片">
            <upload-img @sendFile="user.img = $event" ref='uploadImg'></upload-img>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { mapGetters, mapActions } from "vuex";
import { reqcateadd ,reqcateinfo ,reqcateedit} from "../../../http/api";
import { successAlert,errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins/index";
export default {
  mixins: [formClose],
  props: ["info"],
  data() {
    return {
      user: {
        pid: 0,
        catename: "",
        img: null,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({ list:'cate/list' }),
  },
  mounted() {},
  methods: {
    ...mapActions({ getList:'cate/getList' }),
    //清空表单
    empty() {
      this.user={
        pid: 0,
        catename: "",
        img: null,
        status: 1,
      };
     this.$refs.uploadImg.setImgUrl('');
    },
    //获取一条用户信息
    async getOne(id) {
      let { data: res } = await reqcateinfo({ id });
      console.log(res);
      if (res.code == 200) {
        this.user = res.list;
        this.user.id = id;
        this.$refs.uploadImg.setImgUrl(this.$pre +this.user.img);
      }
    },
    //添加
    async add() {
      let { data: res } = await reqcateadd(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
       this.getList();
      }
    },
    //修改
    async update() {
      let { data: res } = await reqcateedit(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.getList();
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed #ccc;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
  input {
    opacity: 0;
    background-color: transparent;
  }
}
</style>